﻿using FOModels.ConsumerInstallmentLoan;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.ConsumerInstallmentLoan
{
    public class ConsumerInstallmentLoanDAL
    {
       private SQLDataAccess db;
        public ConsumerInstallmentLoanDAL()
        {
            db = new SQLDataAccess();
        }
        public DataTable getTOP1()
        {
            return db.exeSelect("Select TOP 1 * from ConsumerInstallmentLoan order by ConsumerInstallmentLoanID desc");
        }
        public DataTable getall(int langid)
        {
            return db.exeSelect("Select * from ConsumerInstallmentLoan sa join ConsumerInstallmentLoanLang hl on sa.ConsumerInstallmentLoanID = hl.ConsumerInstallmentLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join Language la on hl.LangID = la.ID  where hl.LangID = " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getConsumerInstallmentLoanrelati(int CompanyID, int ConsumerInstallmentLoanID, int langid)
        {
            return db.exeSelect("select * from ConsumerInstallmentLoan ca join ConsumerInstallmentLoanLang hl on ca.ConsumerInstallmentLoanID = hl.ConsumerInstallmentLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.CompanyID = " + CompanyID + " and ca.ConsumerInstallmentLoanID != " + ConsumerInstallmentLoanID + " and Display = 1 and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getConsumerInstallmentLoan(int planid, string balance, int years, int langid)
        {
            return db.exeSelect("Select * from ConsumerInstallmentLoan sa join ConsumerInstallmentLoanLang hl on sa.ConsumerInstallmentLoanID = hl.ConsumerInstallmentLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableConsumerInstallmentLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getConsumerInstallmentLoanByProvider(int planid, string balance, int years, int CompanyID, int langid)
        {
            return db.exeSelect("Select * from ConsumerInstallmentLoan sa join ConsumerInstallmentLoanLang hl on sa.ConsumerInstallmentLoanID = hl.ConsumerInstallmentLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableConsumerInstallmentLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID = " + CompanyID + "");
        }
        public DataTable getConsumerInstallmentLoanDefault(int langid)
        {
            return db.exeSelect("Select * from ConsumerInstallmentLoan sa join ConsumerInstallmentLoanLang hl on sa.ConsumerInstallmentLoanID = hl.ConsumerInstallmentLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableConsumerInstallmentLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getConsumerInstallmentLoanDefaultByProvider(int CompanyID, int langid)
        {
            return db.exeSelect("Select * from ConsumerInstallmentLoan sa join ConsumerInstallmentLoanLang hl on sa.ConsumerInstallmentLoanID = hl.ConsumerInstallmentLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableConsumerInstallmentLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID=" + CompanyID + "");
        }
        public DataTable getConsumerInstallmentLoanByID(int ConsumerInstallmentLoanID, int langid)
        {
            return db.exeSelect("select * from ConsumerInstallmentLoan ca join ConsumerInstallmentLoanLang hl on ca.ConsumerInstallmentLoanID = hl.ConsumerInstallmentLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.ConsumerInstallmentLoanID = " + ConsumerInstallmentLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getConsumerInstallmentLoanByIDManager(int ConsumerInstallmentLoanID, int langid)
        {
            return db.exeSelect("select * from ConsumerInstallmentLoan ca join ConsumerInstallmentLoanLang hl on ca.ConsumerInstallmentLoanID = hl.ConsumerInstallmentLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where ca.ConsumerInstallmentLoanID = " + ConsumerInstallmentLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public int UpdateConsumerInstallmentLoan(ConsumerInstallmentLoanModels models)
        {
            return db.exeUpdate("update ConsumerInstallmentLoan set ConsumerInstallmentLoanName = N'" + models.ConsumerInstallmentLoanName + "', MinimumBalance=" + double.Parse(models.MinimumBalance) + ",MaximumBalance=" + double.Parse(models.MaximumBalance) + ",HomeLoanPlanID=" + models.HomeLoanPlanID + ",CompanyID= " + models.CompanyID + ",Display='" + models.Display + "' where ConsumerInstallmentLoanID = " + models.ConsumerInstallmentLoanID + "");
        }
        public int InsertConsumerInstallmentLoan(ConsumerInstallmentLoanModels models)
        {
            return db.exeUpdate("insert into ConsumerInstallmentLoan values (N'" + models.ConsumerInstallmentLoanName + "'," + models.CompanyID + "," + double.Parse(models.MinimumBalance) + "," + double.Parse(models.MaximumBalance) + "," + models.HomeLoanPlanID + ",'" + models.Display + "')");
        }
        public int DeleteConsumerInstallmentLoan(int ConsumerInstallmentLoanID)
        {
            return db.exeUpdate("Delete from ConsumerInstallmentLoan where ConsumerInstallmentLoanID = " + ConsumerInstallmentLoanID + "");
        }
        public int UpdateConsumerInstallmentLoanLang(ConsumerInstallmentLoanModels models, int langid)
        {
            return db.exeUpdate("update ConsumerInstallmentLoanLang set ConsumerInstallmentLoanName = N'" + models.ConsumerInstallmentLoanName + "', Benefit = N'" + models.Benefit + "',Description=N'" + models.Description + "',FeeContent=N'" + models.FeeContent + "',MoreInfo=N'" + models.MoreInfo + "' where ConsumerInstallmentLoanID = " + models.ConsumerInstallmentLoanID + " and LangID=" + langid + "");
        }
        public int InsertConsumerInstallmentLoanLang(ConsumerInstallmentLoanModels models, int langid)
        {
            return db.exeUpdate("insert into ConsumerInstallmentLoanLang values (" + models.ConsumerInstallmentLoanID + "," + langid + ",N'" + models.ConsumerInstallmentLoanName + "',N'" + models.Benefit + "',N'" + models.Description + "',N'" + models.FeeContent + "',N'" + models.MoreInfo + "')");
        }

        public int DeleteConsumerInstallmentLoanLang(int ConsumerInstallmentLoanID)
        {
            return db.exeUpdate("Delete from ConsumerInstallmentLoanLang where ConsumerInstallmentLoanID = " + ConsumerInstallmentLoanID + "");
        }

        public int DeleteConsumerInstallmentLoanApply(int ConsumerInstallmentLoanApplyID)
        {
            return db.exeUpdate("Delete from ConsumerInstallmentLoanApply where ConsumerInstallmentLoanApplyID = " + ConsumerInstallmentLoanApplyID + "");
        }
        public DataTable getcontractbyid(int ConsumerInstallmentLoanapplyid)
        {
            return db.exeSelect("select * from ConsumerInstallmentLoanApply ca join [ConsumerInstallmentLoan] c on ca.ConsumerInstallmentLoanID = c.ConsumerInstallmentLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.ConsumerInstallmentLoanApplyID = " + ConsumerInstallmentLoanapplyid + " and cl.LangID = 1");
        }
        public DataTable getcontract()
        {
            return db.exeSelect("select * from ConsumerInstallmentLoanApply ca join [ConsumerInstallmentLoan] c on ca.ConsumerInstallmentLoanID = c.ConsumerInstallmentLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and cl.LangID = 1");
        }
        public DataTable getcontractbystatuscount(int statusid)
        {
            return db.exeSelect("select * from ConsumerInstallmentLoanApply ca join [ConsumerInstallmentLoan] c on ca.ConsumerInstallmentLoanID = c.ConsumerInstallmentLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.StatusID = " + statusid + " and cl.LangID = 1");
        }
        public int ConsumerInstallmentLoanApply(int ConsumerInstallmentLoanID, ConsumerInstallmentLoanApplyModels app)
        {
            return db.exeUpdate("insert into ConsumerInstallmentLoanApply(ConsumerInstallmentLoanID,CustomerName,CustomerEmail,CustomerPhone,CustomerIncome,CustomerDOB,StatusID) values (" + ConsumerInstallmentLoanID + ",N'" + app.CustomerName + "',N'" + app.CustomerEmail + "',N'" + app.CustomerPhone + "',N'" + app.CustomerIncome + "',N'" + app.CustomerDOB + "'," + app.StatusID + ")");
        }
        public int UpdateStatus(ConsumerInstallmentLoanApplyModels ConsumerInstallmentLoan)
        {
            return db.exeUpdate("update ConsumerInstallmentLoanApply set StatusID= " + ConsumerInstallmentLoan.StatusID + " where ConsumerInstallmentLoanApplyID = " + ConsumerInstallmentLoan.ConsumerInstallmentLoanApplyID + "");
        }
    }
}
